package com.chinasoft.rentalservice.service.impl;

import com.chinasoft.feignclient.client.CarClient;
import com.chinasoft.ms.dto.RentalDTO;
import com.chinasoft.ms.dto.RentalRecordDTO;
import com.chinasoft.ms.pojo.PageItem;
import com.chinasoft.ms.pojo.Rental;
import com.chinasoft.ms.pojo.RentalRecord;
import com.chinasoft.rentalservice.mapper.RentalMapper;
import com.chinasoft.rentalservice.service.RentalService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class RentalServiceImpl implements RentalService {

    @Autowired
    private RentalMapper rentalMapper;

    @Autowired
    CarClient carClient;
    @Override
    @Transactional
    public PageItem<RentalRecordDTO> queryByPageCond(Integer pageNum, Integer pageSize, RentalRecordDTO query) {
        PageHelper.startPage(pageNum, pageSize);

        RentalRecord condition = new RentalRecord();
        if (query != null) {
            condition.setRentalId(query.getRentalId());
            condition.setCarId(query.getCarId());
            condition.setUserId(query.getUserId());
            condition.setRentalStart(query.getRentalStart());
            condition.setRentalEnd(query.getRentalEnd());
            condition.setStatus(query.getStatus());
        }

        List<RentalRecord> records = rentalMapper.selectRecordByCondition(condition);
        PageInfo<RentalRecord> pageInfo = new PageInfo<>(records);

        List<RentalRecordDTO> dtoList = records.stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());

        return new PageItem<>(
                dtoList,
                pageInfo.getTotal(),
                pageInfo.getPageNum(),
                pageInfo.getPageSize()
        );
    }

    @Override
    @Transactional
    public boolean addRental(RentalRecordDTO rentalRecordDTO) {
        Rental rental = new Rental();
        rental.setCarId(rentalRecordDTO.getCarId());
        rental.setUserId(rentalRecordDTO.getUserId());
        rental.setRentalStart(rentalRecordDTO.getRentalStart());
        rental.setRentalEnd(rentalRecordDTO.getRentalEnd());
        rental.setExtensionCount(0); // 初始续租次数为0

        int rentalResult = rentalMapper.insert(rental);

        RentalRecord rentalRecord = new RentalRecord();
        rentalRecord.setRentalId(rental.getId());
        rentalRecord.setCarId(rentalRecordDTO.getCarId());
        rentalRecord.setUserId(rentalRecordDTO.getUserId());
        rentalRecord.setRentalStart(rentalRecordDTO.getRentalStart());
        rentalRecord.setRentalEnd(rentalRecordDTO.getRentalEnd());
        rentalRecord.setStatus("0"); // 初始状态为未归还
        rentalRecord.setRentalCount(1); // 初始租赁次数为1

        int recordResult = rentalMapper.insertRecord(rentalRecord);

        return rentalResult > 0 && recordResult > 0;
    }

    @Override
    @Transactional
    public boolean updateRental(RentalRecordDTO rentalRecordDTO) {
        Rental rental = new Rental();
        rental.setId(rentalRecordDTO.getRentalId());
        rental.setCarId(rentalRecordDTO.getCarId());
        rental.setUserId(rentalRecordDTO.getUserId());
        rental.setRentalStart(rentalRecordDTO.getRentalStart());
        rental.setRentalEnd(rentalRecordDTO.getRentalEnd());

        RentalRecord rentalRecord = new RentalRecord();
        rentalRecord.setRentalId(rentalRecordDTO.getRentalId());
        rentalRecord.setCarId(rentalRecordDTO.getCarId());
        rentalRecord.setUserId(rentalRecordDTO.getUserId());
        rentalRecord.setRentalStart(rentalRecordDTO.getRentalStart());
        rentalRecord.setRentalEnd(rentalRecordDTO.getRentalEnd());
        rentalRecord.setStatus(rentalRecordDTO.getStatus());
        rentalRecord.setRentalCount(rentalRecordDTO.getRentalCount());

        int rentalResult = rentalMapper.update(rental);
        int recordResult = rentalMapper.updateRecord(rentalRecord);

        boolean b = carClient.updateCarStatus(rentalRecordDTO.getCarId(), rentalRecordDTO.getStatus());


        return rentalResult > 0 && recordResult > 0;
    }

    @Override
    @Transactional
    public boolean deleteRental(Long id) {
        int recordResult = rentalMapper.deleteRecordById(id);
        int rentalResult = rentalMapper.deleteById(id);
        return rentalResult > 0 && recordResult > 0;
    }

    @Override
    @Transactional
    public boolean deleteBatch(List<Long> ids) {
        int recordResult = rentalMapper.deleteBatchRecords(ids);
        return recordResult > 0;
    }

    @Override
    public RentalRecordDTO getRentalByCarId(Long carId) {
        System.out.println("Querying rental record for carId: {}"+ carId);
        RentalRecord rentalRecord = rentalMapper.selectRecordByCarId(carId);
        return convertToDTO(rentalRecord);
    }
    @Override
    public RentalRecordDTO getRental2ByCarId(Long carId) {
        System.out.println("Querying rental record for carId: {}"+ carId);
        RentalRecord rentalRecord = rentalMapper.selectRecord2ByCarId(carId);
        return convertToDTO(rentalRecord);
    }
    @Override
    public boolean updateStatus(Long id, String status) {
        // 获取当前日期和时间，使用UTC时区
        ZonedDateTime utcDateTime = ZonedDateTime.now(ZoneId.of("UTC"));

        // 将ZonedDateTime转换为Instant
        Instant instant = utcDateTime.toInstant();

        // 将Instant转换为java.util.Date
        Date date = Date.from(instant);
        System.out.println("date:"+date);
        rentalMapper.updateRentalEnd(id, date);
        rentalMapper.updateRentalEnd2(id, date);
        return rentalMapper.updateStatus(id, status) > 0;
    }

    @Override
    public RentalDTO getRentalById(Long rentalId) {

        Rental rental = rentalMapper.selectById(rentalId);
        RentalDTO rentalDTO = convertToDTO(rental);
        return rentalDTO;
    }

    @Override
    public Rental QueryByCarId(Long carId) {
        System.out.println("Querying rental record for carId: {}"+ carId);
        return rentalMapper.selectByCarId(carId);
    }

    public RentalRecordDTO convertToDTO(RentalRecord rentalRecord) {
        if (rentalRecord == null) {
            return null;
        }
        RentalRecordDTO dto = new RentalRecordDTO();
        dto.setRentalId(rentalRecord.getRentalId());
        dto.setCarId(rentalRecord.getCarId());
        dto.setUserId(rentalRecord.getUserId());
        dto.setRentalStart(rentalRecord.getRentalStart());
        dto.setRentalEnd(rentalRecord.getRentalEnd());
        dto.setStatus(rentalRecord.getStatus());
        dto.setRentalCount(rentalRecord.getRentalCount());
        return dto;
    }


    public RentalDTO convertToDTO(Rental rental) {
        if (rental == null) {
            return null;
        }
        RentalDTO dto = new RentalDTO();
        dto.setId(rental.getId());
        dto.setCarId(rental.getCarId());
        dto.setUserId(rental.getUserId());
        dto.setRentalStart(rental.getRentalStart());
        dto.setRentalEnd(rental.getRentalEnd());
        return dto;
    }
    public RentalRecord convertToEntity(RentalRecordDTO dto) {
        if (dto == null) {
            return null;
        }
        RentalRecord rentalRecord = new RentalRecord();
        rentalRecord.setRentalId(dto.getRentalId());
        rentalRecord.setCarId(dto.getCarId());
        rentalRecord.setUserId(dto.getUserId());
        rentalRecord.setRentalStart(dto.getRentalStart());
        rentalRecord.setRentalEnd(dto.getRentalEnd());
        rentalRecord.setStatus(dto.getStatus());
        rentalRecord.setRentalCount(dto.getRentalCount());
        return rentalRecord;
    }
}
